<?php

if(!defined('IN_SYS')) exit('Access Denied');

class dashboard_controller extends admincp{

	public function __construct(){
		parent::__construct();
	}

	public function index(){

		$yumingCount = db::getfield("select count(*) from yuming");

		$today = date("Ymd");
		$todaystart = strtotime($today);

		// 统计文章数
		$spiderCount = $newsCount = $todayCount = 0;
		if($this->allyuming){
			$countSql = array();
			foreach ($this->allyuming as $key => $value) {
				$countSql[] = "select sum(case when addtime>$todaystart then 1 else 0 end) as todaycount, count(*) as tmpcount from content_{$key}";

				//爬虫
				$site_spider =  DATA.DS.'spider'.DS.$key.DS.'spider.php';
				if(is_file($site_spider)) {
					$spider = require $site_spider;
					
					$spiderCount += isset($spider[$today]) ? array_sum($spider[$today]) : 0;
				}
			}
			$countSql = join(" union all ", $countSql);
			$countSql = "select sum(todaycount) as todaycount,sum(tmpcount) as tmpcount from ($countSql) a";
			$res = db::find($countSql);
			
			$newsCount = empty($res['tmpcount']) ? 0 : $res['tmpcount'];
			$todayCount = empty($res['todaycount']) ? 0 : $res['todaycount'];
		}

		$allymstr = array();
		foreach ($this->allyuming as $key => $value) {
			$allymstr[] = trim($value['name']);
		}
		$allym = implode(",", $allymstr);
		$memory_limit = ini_get('memory_limit');
		$free_space = formatsize(disk_free_space(dirname(dirname(__FILE__))));

                $jscdata = jsc::count_today_push();
		$visitCount = db::getField("select varValue from config where varName='visitCount'");
                
		require self::tpl("dashboard");
	}

	public function submenu(){
		$menuId = gp('menuId');
		echo $this->loadLeftMenu($menuId);
	}

	public function redirectindex(){
		if($this->currentYuming){
			header("Location: http://{$this->currentYuming['name']}");
		}else{
			header("Location: /");
		}
	}
        
//        public function test() {
//            for($i=1;$i<=100;$i++) {
//                mredis::zAdd('baidupush', rand(10000,1000000), $i);
//            }
//        }
        
        
        public function get_count_lists() {
            $jscdata = jsc::count_today_push();
            $jsc_pai_list = [];
            if(!empty($jscdata['lists'])) {
                foreach($jscdata['lists'] as $key=>$v) {
                    $yuming = db::find('select * from `yuming` where id='.$key);
                    $jsc_pai_list[]=['id'=>$yuming['id'],'name'=>$yuming['name'],'count'=>(5000000-intval($v))];
                }
            }
            
            $testd = [];
            foreach($jsc_pai_list as $key=>$v) {
                $testd['x'][] = $v['name'];
                $testd['y'][] = $v['count'];
                $testd['y_count'][] = $v['name'];
                $testd['y_bad'][] = $v['id'];
                $testd['color_list'][] = "rgb(".rand(10,255).", ".rand(10,255).", ".rand(1,100).")";
            }
            self::json( array('status' => 1, 'data' => $testd));
        }

}

